﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class Admin_Default2 : System.Web.UI.Page
{
    clsDBOperation objDBOp = new clsDBOperation();
    Hashtable hstParameters = new Hashtable();
    DataSet dsadmin = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["dsGolbal"] == null)
        {
            Response.Redirect("../");
        }
        dsadmin = (DataSet)Session.Contents["dsGolbal"];
        if (!IsPostBack)
        {
            FillSongCategory();
        }
    }

    private void FillSongCategory()
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Fill_Song_Category");
            dssave = (DataSet)objDBOp.GetDataset("sp_SongAlbum", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {

                ddlSongCategory.DataTextField = "SubCategoryName";
                ddlSongCategory.DataValueField = "SubCategoryID";
                ddlSongCategory.DataSource = dssave;
                ddlSongCategory.DataBind();
                ddlSongCategory.Items.Insert(0, new ListItem("---Select Song Category---", "0"));
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }

    protected void ddlSongCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillAlbum();

    }


    private void FillAlbum()
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Fill_Album");
            hstParameters.Add("FK_SubCategoryID", Convert.ToInt32(ddlSongCategory.SelectedValue));
            dssave = (DataSet)objDBOp.GetDataset("sp_SongAlbum", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {

                ddlAlbum.DataTextField = "AlbumName";
                ddlAlbum.DataValueField = "AlbumID";
                ddlAlbum.DataSource = dssave;
                ddlAlbum.DataBind();
                ddlAlbum.Items.Insert(0, new ListItem("---Select Album---", "0"));
            }
            else
            {

                ddlAlbum.Items.Clear();
                ddlAlbum.Items.Insert(0, new ListItem("---Album Not Found---", "0"));

            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }

    protected void ddlAlbum_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSongLink();
    }

    private void GetSongLink()
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_Song_Link");
            hstParameters.Add("FK_AlbumID", Convert.ToInt32(ddlAlbum.SelectedValue));
            dssave = (DataSet)objDBOp.GetDataset("sp_SongAlbum", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {
                grdSong.DataSource = dssave;
                grdSong.DataBind();
                lblmsg.Text = string.Empty;
            }
            else
            {
                lblmsg.Text = "No Records Found For Album  " + ddlAlbum.SelectedItem.Text;
                grdSong.DataSource = null;
                grdSong.DataBind();

            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }

    protected void btnAddCategory_Click(object sender, EventArgs e)
    {
        Save();
        GetSongLink();
    }

    private void Save()
    {
        try
        {
            

            int IsSaved;
            hstParameters.Clear();
            hstParameters.Add("Action", "Insert_Song_Link");
            hstParameters.Add("UserName", dsadmin.Tables[0].Rows[0][1].ToString());
            hstParameters.Add("FK_AlbumID", Convert.ToInt32(ddlAlbum.SelectedValue));
            hstParameters.Add("SongName", txtSongTitle.Text);
            hstParameters.Add("Artist", txtArtistName.Text);
            hstParameters.Add("DownloadLink", txtDownloadLink.Text);
            hstParameters.Add("LinkOwner", txtLinkOwner.Text);

            IsSaved = Convert.ToInt32(objDBOp.ExecuteNonQuery("sp_SongAlbum ", CommandType.StoredProcedure, hstParameters));
            if (IsSaved > 0)
                lblmsg.Text = "Record Saved Successfully";
            txtSongTitle.Text = string.Empty;
            txtArtistName.Text = string.Empty;
            txtDownloadLink.Text = string.Empty;
            

        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }
}
